<template>
  <el-card style="margin: 10px 0px">
    <div class="top">
      <!--v-model收集选项卡的数据-->
      <el-tabs v-model="activeName">
        <el-tab-pane label="访问量" name="visite"> </el-tab-pane>
        <el-tab-pane label="销售额" name="sale"> </el-tab-pane>
      </el-tabs>
      <div class="right">
        <span @click="setToday">今日</span>
        <span @click="setWeek">本周</span>
        <span @click="setMonth">本月</span>
        <span @click="setYear">本年</span>
        <el-date-picker
          v-model="riliArr"
          size="mini"
          type="daterange"
          range-separator="--"
          start-placeholder="开始入住日期"
          end-placeholder="结束离开日期"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </div>
    </div>
    <div class="bottom">
      <el-row :gutter="20">
        <el-col :span="16" :offset="0">
          <!-- 展示柱状图的地方:准备一个容器 -->
          <div class="bar" ref="bar"></div>
        </el-col>
        <el-col :span="8" :offset="0">
          <!--滚动组件地方-->
          <Scroll
            :data="listData"
            :class-option="optionSingleHeight"
            class="seamless-warp"
          >
            <ul class="item">
              <li v-for="(item, index) in listData" :key="index">
                <span class="title" v-text="item.title"></span>
                <span class="date" v-text="item.date"></span>
              </li>
            </ul>
          </Scroll>
        </el-col>
      </el-row>
    </div>
  </el-card>
</template>

<script>
//日期相关的插件dayjs|moment
import dayjs from "dayjs";
//滚动组件
import Scroll from "vue-seamless-scroll";

//引入echarts
import * as echarts from "echarts";
export default {
  name: "",
  data() {
    return {
      //滚动的假的数据
      listData: [
        {
          title: "无缝滚动第一行无缝滚动第一行",
          date: "2017-12-16",
        },
        {
          title: "无缝滚动第二行无缝滚动第二行",
          date: "2017-12-16",
        },
        {
          title: "无缝滚动第三行无缝滚动第三行",
          date: "2017-12-16",
        },
        {
          title: "无缝滚动第四行无缝滚动第四行",
          date: "2017-12-16",
        },
        {
          title: "无缝滚动第五行无缝滚动第五行",
          date: "2017-12-16",
        },
        {
          title: "无缝滚动第六行无缝滚动第六行",
          date: "2017-12-16",
        },
        {
          title: "无缝滚动第七行无缝滚动第七行",
          date: "2017-12-16",
        },
        {
          title: "无缝滚动第八行无缝滚动第八行",
          date: "2017-12-16",
        },
        {
          title: "无缝滚动第九行无缝滚动第九行",
          date: "2017-12-16",
        },
      ],
      activeName: "visite",
      //收集日历的数据
      riliArr: [],
      //VC身上属性,存储echarts实例
      mycharts: null,
      //存储柱状图来自于服务器的数据
      barData: {},
    };
  },
  //组件挂载完毕,初始化图标
  mounted() {
    //初始化柱状图
    this.setCharts();

    window.addEventListener("resize", () => {
      //图标自适应
      this.mycharts.resize();
    });

    //模拟发ajax请求
    setTimeout(() => {
      this.barData = {
        data: {
          visite: [45, 78, 12, 67, 36, 90, 123],
          sale: [99, 23, 82, 123, 54, 29, 77],
        },
      };
    }, 500);
  },
  methods: {
    //今日
    setToday() {
      //市场当中:关于日期相关的出名插件moment|dayjs,用法基础差不多!!!
      //获取今日的时间
      let start = dayjs().format("YYYY-MM-DD");
      let end = dayjs().format("YYYY-MM-DD");
      this.riliArr = [start, end];
    },
    //本周日期
    setWeek() {
      //获取本周周一日期与周日的日期
      let start = dayjs().day(1).format("YYYY-MM-DD");
      let end = dayjs().day(7).format("YYYY-MM-DD");
      this.riliArr = [start, end];
    },
    //本月月初与月末日期多少号
    setMonth() {
      //本月开始日期与结束日期
      let start = dayjs().startOf("month").format("YYYY-MM-DD");
      let end = dayjs().endOf("month").format("YYYY-MM-DD");
      this.riliArr = [start, end];
    },
    //本年开始日期与本年结束日期
    setYear() {
      let start = dayjs().startOf("year").format("YYYY-MM-DD");
      let end = dayjs().endOf("year").format("YYYY-MM-DD");
      this.riliArr = [start, end];
    },
    //图形图标的方法
    setCharts() {
      //初始化echarts实例
      this.mycharts = echarts.init(this.$refs.bar);
      //配置项
      let option = {
        //标题
        title: {
          text: "访问量",
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "5%",
          containLabel: true,
        },
        xAxis: [
          {
            type: "category",
            data: ["苹果", "华为", "vivo", "oppo", "三星", "长虹", "诺基亚"],
            axisTick: {
              alignWithLabel: true,
            },
            name: "全世界手机排名",
            nameLocation: "center",
            nameGap: 30,
          },
        ],
        yAxis: [
          {
            type: "value",
          },
        ],
        series: [
          {
            name: "Direct",
            type: "bar",
            barWidth: "60%",
            data: [],
          },
        ],
      };
      //设置配置项
      this.mycharts.setOption(option);
    },
  },
  components: {
    Scroll,
  },
  computed: {
    optionSingleHeight() {
      return {
        singleHeight: 40,
      };
    },
  },
  //监听
  watch: {
    //监听选项卡属性值的变化
    activeName() {
      //监听到属性值的变化，重新配置配置项
      //设置配置项
      this.mycharts.setOption({
        //只是修改标题[其余的配置项没有修改，还是使用最开始的配置项]
        title: {
          text: this.activeName == "visite" ? "访问量" : "销售额",
        },
        series: [
          {
            name: "Direct",
            type: "bar",
            barWidth: "60%",
            data:
              this.activeName == "visite"
                ? this.barData.data.visite
                : this.barData.data.sale,
          },
        ],
      });
    },
    //监听服务器的数据是否回来
    barData() {
      //设置配置项:将柱状图的数据空数组替换为服务器的数组的数据
      this.mycharts.setOption({
        series: [
          {
            name: "Direct",
            type: "bar",
            barWidth: "60%",
            data: this.barData.data.visite,
          },
        ],
      });
    },
  },
};
</script>



<style scoped lang="scss">
.seamless-warp {
  height: 400px;
  overflow: hidden;
}

.bar {
  width: 100%;
  height: 400px;
}

.item {
  list-style: none;
  li {
    height: 40px;
    line-height: 40px;
    color: yellowgreen;
  }
}

.top {
  position: relative;
  .right {
    position: absolute;
    right: 0px;
    top: 0px;
    span {
      margin: 0px 10px;
    }
  }
}
</style>
